<template>
  <f7-page>
    <f7-navbar title="吉林动画学院用人申请表"></f7-navbar>
    <f7-list inline-labels no-hairlines-md>
      <f7-list-item>
        <f7-label>名称</f7-label>
        <f7-input type="text" :value="formContent.title" @input="formContent.title = $event.target.value"></f7-input>
      </f7-list-item>

      <f7-list-item  >
        <f7-label>申请部门</f7-label>
        <f7-input type="select" placeholder="Please choose..." :value="formContent.orgName"
                  @input="formContent.orgName = $event.target.value">
          <option  v-for="item in orgNames" v-bind:key="item.name.id" v-bind:value="item.name" >{{item.name}}</option>
        </f7-input>
      </f7-list-item>
      <f7-list-item >
        <f7-label>申请事由</f7-label>
        <f7-input type="select" placeholder="Please choose..." :value="formContent.reason"
                  @input="formContent.reason = $event.target.value">
          <option v-for="(value, index) in resonNum" v-bind:key="index" v-bind:value="index">{{value}}</option>
        </f7-input>
      </f7-list-item>
      <f7-list-item>
        <f7-label>职位名称</f7-label>
        <f7-input type="text" :value="formContent.positionName" @input="formContent.positionName = $event.target.value"></f7-input>
      </f7-list-item>
      <f7-list-item  >
        <f7-label>拟招聘人数</f7-label>
        <f7-input  type="tel" placeholder="" clear-button :value="formContent.recruitCount"
                   @input="formContent.recruitCount = $event.target.value"></f7-input>
      </f7-list-item>
      <f7-list-item>
        <f7-label>定编人数</f7-label>
        <f7-input type="tel" clear-button :value="formContent.allocationCount"
                  @input="formContent.allocationCount = $event.target.value"></f7-input>
      </f7-list-item>
      <f7-list-item>
        <f7-label>现有人数</f7-label>
        <f7-input type="tel"  clear-button :value="formContent.existCount"
                  @input="formContent.existCount = $event.target.value"></f7-input>
      </f7-list-item>

      <f7-list-item >
        <f7-label>核心职责</f7-label>
        <f7-input type="textarea" placeholder="Input here please" :value="formContent.coreRecruit"
                  @input="formContent.coreRecruit = $event.target.value"></f7-input>
      </f7-list-item>
      <f7-list-item >
        <f7-label>常规职责</f7-label>
        <f7-input type="textarea" placeholder="Input here please" :value="formContent.commonRecruit"
                  @input="formContent.commonRecruit = $event.target.value"></f7-input>
      </f7-list-item>
      <f7-list-item >
        <f7-label>任职基本条件</f7-label>
        <f7-input type="textarea" placeholder="Input here please" :value="formContent.baseCondition"
                  @input="formContent.baseCondition = $event.target.value"></f7-input>
      </f7-list-item>


      <f7-list-item>
        <f7-label>待遇标准</f7-label>
        <f7-input type="text" :value="formContent.treatmentLevel" @input="formContent.treatmentLevel = $event.target.value"></f7-input>
      </f7-list-item>
      <f7-list-item>
        <f7-label>建议招聘方式</f7-label>
        <f7-input type="select" placeholder="Please choose..." :value="formContent.recruitType"
                  @input="formContent.recruitType = $event.target.value">
          <option v-for="(value, index) in recruitChannelNum" v-bind:key="index" v-bind:value="index">{{value}}</option>
        </f7-input>
      </f7-list-item>
    </f7-list>

    <f7-block strong>
      <f7-row>
        <f7-col width="50">
          <f7-button fill raised @click="submitApply">提交</f7-button>
        </f7-col>
        <f7-col width="50">
          <f7-button fill raised back>取消</f7-button>
        </f7-col>
      </f7-row>
    </f7-block>


  </f7-page>
</template>

<script>
    export default {
      name: "applyRecruitTable",

      data() {
        return {
          userId: sessionStorage.getItem('userId'),
          resonNum: this.GLOBAL.resonNum,
          orgNames: this.GLOBAL.orgNames,
          recruitChannelNum: this.GLOBAL.recruitChannelNum,
          pastMemory: {},
          formContent: {
            title: "吉林动画学院用人申请表",
            createBy: sessionStorage.getItem('userName'),
            baseCondition:"",
            allocationCount:0,
            commonRecruit:"",
            coreRecruit:"",
            existCount:0,
            manpower:"",
            orgName: "动画工程中心",
            orgId: 20769,
            positionName:"",
            reason:0,
            recruitCount: 0,
            recruitType:0,
            treatmentLevel: "",
            auditResult: "流程尚未启动",
            depUser:"",
            creationDate:"",
          }
        }
      },
      created() {
        this.$f7ready((f7) => {
          let that = this;
          f7.request.setup({
            headers: {
              'AccessToken': sessionStorage.getItem('accessToken'),
              'JSESSIONID': sessionStorage.getItem('userToken'),
            }
          });
          // f7.request.json(that.GLOBAL.RequestAddress + '/camel/rest/users/' + that.userId + "/orgs",{
          //     'page':1,
          //     'start':0,
          //     'limit':20
          //   }, function(res){
          //     if(res.totalCount>0){
          //       that.orgNames = res.data;
          //     }
          //   }
          // );
          if (that.$f7route.params.id > -1 )  {
            f7.request.json(that.GLOBAL.RequestAddress + '/camel/rest/recruitapplys', {
              'page': 1,
              'limit': 10,
              'jsonStr': '',
              'otherStr': '',
              'start': '0'
            }, function (result) {
              if (result.totalCount > 0) {
                for (let pm of result.data) {
                  if (pm.id == that.$f7route.params.id) {
                    that.pastMemory = pm;
                    break;
                  }
                }
                if (Object.keys(that.pastMemory).length === 0) {
                  return;
                } else {
                  that.formContent.title = that.pastMemory.title;
                  that.formContent.createBy = that.pastMemory.createBy;
                  that.formContent.baseCondition = that.pastMemory.baseCondition;
                  that.formContent.allocationCount = that.pastMemory.allocationCount;
                  that.formContent.commonRecruit = that.pastMemory.commonRecruit;
                  that.formContent.coreRecruit = that.pastMemory.coreRecruit;
                  that.formContent.existCount = that.pastMemory.existCount;
                  that.formContent.manpower = that.pastMemory.manpower;
                  that.formContent.orgName = that.pastMemory.orgName;
                  that.formContent.orgId = that.pastMemory.orgId;
                  that.formContent.positionName = that.pastMemory.positionName;
                  that.formContent.reason = that.pastMemory.reason;
                  that.formContent.recruitCount = that.pastMemory.recruitCount;
                  that.formContent.recruitType = that.pastMemory.recruitType;
                  that.formContent.treatmentLevel = that.pastMemory.treatmentLevel;
                  that.formContent.auditResult = that.pastMemory.auditResult;
                  that.formContent.depUser = that.pastMemory.depUser;
                  that.formContent.creationDate = that.pastMemory.creationDate;
                }
                ;
              }
            })
          }

        });
      },
      methods: {
        submitApply: function (event) {
          let that = this;
          let ff = that.formContent;
          let app = that.$f7;

          if(that.checkValidate() ==false) {
            return
          }

          that.findIDFromOrgName(that.formContent.orgName);

          if (Object.keys(that.pastMemory).length === 0) {
            that.findIDFromOrgName(that.formContent.orgName);
            app.request.postJSON(that.GLOBAL.RequestAddress + '/camel/rest/recruitapplys', ff, function (result) {
              that.showAlert("提示", result.msg)
            }, function(err ) {
              app.dialog.alert("添加新纪录异常，请重新添加", "提示");
            });
          } else {
            ff.id = that.pastMemory.id;
            let url = that.GLOBAL.RequestAddress + '/camel/rest/recruitapplys/' + that.pastMemory.id;
            that.putRequest(url, ff);
          }
        },
        showAlert: function (title, text) {
          let that = this;
          let app = that.$f7;
          app.dialog.alert(text, title, () => {
            that.$f7router.back();
          });
        },
        findIDFromOrgName: function (name) {
          let that = this;
          for(let item of that.orgNames) {
            if(item.name.localeCompare(name) == 0){
              that.formContent.orgId = item.id;
              that.formContent.orgName = item.name;
              break;
            }
          }
          return;
        },
        putRequest: function (url, cargo) {
          let that = this;
          fetch(url,
            {method: "put",
              headers: {
                "Content-Type": "application/json;charset=UTF-8",
                'AccessToken': sessionStorage.getItem('accessToken'),
                'JSESSIONID': sessionStorage.getItem('userToken'),
              },
              body: JSON.stringify(cargo)
            }).then (response => response.json())
            .then(  result => {
              console.log(result)
              that.showAlert("提示", result.msg)
          }).catch(function(err){
            console.log("Fetch错误",err)
          });
        },
        checkValidate: function () {
          let that = this;
          let app = that.$f7;

          if(that.formContent.positionName.length < 1) {
            app.dialog.alert("请填写职位名称", "提示", null);   return false
          } else if(that.formContent.recruitCount < 1) {
            app.dialog.alert("请填写拟聘人数", "提示", null);   return false
          } else if(that.formContent.allocationCount < 1) {
            app.dialog.alert("请填写定编人数", "提示", null);   return false
          } else if(that.formContent.existCount < 1) {
            app.dialog.alert("请填写现有人数", "提示", null);   return false
          } else if(that.formContent.coreRecruit.length < 1) {
            app.dialog.alert("请填写核心职责", "提示", null);   return false
          } else if(that.formContent.treatmentLevel.length < 1) {
            app.dialog.alert("请填写待遇标准", "提示", null);   return false
          }
          return true;
        }
      },
    }
</script>

<style scoped>

</style>
